﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_Person_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        ucCases.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(ucCases_SelectData);
        ucLatestNews.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(ucLatestNews_SelectData);
        ucTestamonialModule.DataRequired += new EventHandler(ucTestamonialModule_DataRequired);
        acmsEmail.DataBound += new EventHandler(acmsEmail_DataBound);
        acmsTel.DataBound += new EventHandler(acmsTel_DataBound);
        acmsDownload1.DataBound += new EventHandler(acmsDownload1_DataBound);
        acmsDownload2.DataBound += new EventHandler(acmsDownload2_DataBound);
        acmsImage.DataBound += new EventHandler(acmsImage_DataBound);
        acmsPosition.DataBound += new EventHandler(acmsPosition_DataBound);
    }

    void acmsPosition_DataBound(object sender, EventArgs e)
    {
        if (acmsPosition.IsEmpty)
        {
            liPosition.Visible = false;
        }
    }

    void acmsImage_DataBound(object sender, EventArgs e)
    {
        if (acmsImage.IsEmpty)
        {
            divImage.Visible = false;
        }
    }

    void acmsDownload1_DataBound(object sender, EventArgs e)
    {
        if (acmsDownload1.IsEmpty)
        {
            liDownload1.Visible = false;
        }
    }

    void acmsDownload2_DataBound(object sender, EventArgs e)
    {
        if (acmsDownload2.IsEmpty)
        {
            liDownload2.Visible = false;
        }
    }

    void acmsTel_DataBound(object sender, EventArgs e)
    {
        if (acmsTel.IsEmpty)
        {
            liPhone.Visible = false;
        }
    }

    void acmsEmail_DataBound(object sender, EventArgs e)
    {
        if (acmsEmail.IsEmpty)
        {
            liEmail.Visible = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (AcmsContext.CurrentPost != null)
            {
                LinkTopic = Topic.GetBySiteIdTopicGroupPathAndName(AcmsContext.SiteId, "People", AcmsContext.CurrentPost.DisplayName);
            }
            DataBind();
        }
    }

    private int linkTopicId;
    private Topic linkTopic;
    protected Topic LinkTopic
    {
        get
        {
            if (linkTopic == null && linkTopicId > 0)
            {
                linkTopic = Topic.GetById(linkTopicId);
            }
            return linkTopic;
        }
        set
        {
            if (value != null)
            {
                linkTopicId = value.Id;
            }
            linkTopic = value;
        }
    }

    protected override object SaveViewState()
    {
        ViewState["LinkTopicId"] = linkTopicId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["LinkTopicId"] is int)
        {
            linkTopicId = (int)ViewState["LinkTopicId"];
        }
    }

    protected override void DataBindChildren()
    {
        List<Topic> services = null;
        List<Topic> sectors = null;
        if (AcmsContext.CurrentPost != null)
        {
            services = Topic.GetAllByPostIdAndTopicGroupPath(AcmsContext.CurrentPost.Id, "Services");
            sectors = Topic.GetAllByPostIdAndTopicGroupPath(AcmsContext.CurrentPost.Id, "Sectors");
        }
        else
        {
            services = new List<Topic>();
            sectors = new List<Topic>();
            while(services.Count < 5 && sectors.Count < 5)
            {
                services.Add(Topic.CreateInstance());
                services.Last().Name = string.Format("Service Topic {0}", services.Count());
                sectors.Add(Topic.CreateInstance());
                sectors.Last().Name = string.Format("Sector Topic {0}", sectors.Count());
            }
        }
        if (services.Count > 0)
        {
            listServices.DataSource = services;
        }
        else
        {
            listServices.Visible = false;
        }
        if (sectors.Count > 0)
        {
            listSectors.DataSource = sectors;
        }
        else
        {
            listSectors.Visible = false;
        }

        base.DataBindChildren();
    }


    void ucTestamonialModule_DataRequired(object sender, EventArgs e)
    {
        if (LinkTopic != null)
        {
            ucTestamonialModule.DataSource = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, LinkTopic.Id, "~/Testamonials", AcmsContext.SiteVersion);
        }
    }

    void ucLatestNews_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        if (LinkTopic != null)
        {
            e.SetData(Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, LinkTopic.Id, "~/News", AcmsContext.SiteVersion));
        }
    }

    void ucCases_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        if (LinkTopic != null)
        {
            e.SetData(Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, LinkTopic.Id, "~/Cases", AcmsContext.SiteVersion));
        }
    }
}
